const router = require("koa-router")()

router.prefix("/admin")

const { GM, FoodGM, SelfDone, ManBing, FamilyBing, KeShi } = require("../models/dangan")

const {CtxBody} = require("../models/config")

// --------------------------------------------------------------- 慢性病 -------------

// 添加慢性病

router.post("/addManBing", async ctx => {
    let { name, id } = ctx.request.body 
    if (id) {
        let data = await ManBing.findOne({ name })
        if (data) {
            CtxBody(ctx, 402, "")
        } else {
            let res = await ManBing.findOneAndUpdate({_id: id}, {
                $set: {
                    name
                }
            })
            CtxBody(ctx, 204, "")
        }
        
    } else {
        let gm = new ManBing({
            name
        })
        let ret = await gm.save()
        CtxBody(ctx, 202, "")
    }
})

// 显示或隐藏慢性病

router.post("/changeManStatus", async ctx => {
    let { id } = ctx.request.body 
    let man = await ManBing.findOne({ _id: id })
    let state = man.status
    if (state == 1) {
        let update = await ManBing.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "2"
            }
        })
        CtxBody(ctx, 204, "")
    } else {
        let update = await ManBing.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "1"
            }
        })
        CtxBody(ctx, 204, "")
    }
})

// 获取慢性病

router.post("/getManBing", async ctx => {
    let { page } = ctx.request.body 
    let SkipPage = page ? page : 1
    let pageSize = 10
    let count = await ManBing.count()
    let res = await ManBing.find().skip( (SkipPage - 1) * pageSize)
    if (res) {
        CtxBody(ctx, 200, {res, count})
    } else {
        CtxBody(ctx, 404, "")
    }
})
// --------------------------------------------------------------------------------------

// --------------------------------------------------------------- 家族病 -------------

// 添加家族病

router.post("/addFamilyBing", async ctx => {
    let { name, id } = ctx.request.body 
    if (id) {
        let data = await FamilyBing.findOne({ name })
        if (data) {
            CtxBody(ctx, 402, "")
        } else {
            let res = await FamilyBing.findOneAndUpdate({_id: id}, {
                $set: {
                    name
                }
            })
            CtxBody(ctx, 204, "")
        }
    } else {
        let gm = new FamilyBing({
            name
        })
        let ret = await gm.save()
        CtxBody(ctx, 202, "")
    }
})

// 显示或隐藏家族病

router.post("/changeFamilyStatus", async ctx => {
    let { id } = ctx.request.body 
    let man = await FamilyBing.findOne({ _id: id })
    let state = man.status
    if (state == 1) {
        let update = await FamilyBing.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "2"
            }
        })
        CtxBody(ctx, 204, "")
    } else {
        let update = await FamilyBing.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "1"
            }
        })
        CtxBody(ctx, 204, "")
    }
})

// 获取家族病

router.post("/getFamilyBing", async ctx => {
    let { page } = ctx.request.body 
    let SkipPage = page ? page : 1
    let pageSize = 10
    let count = await FamilyBing.count()
    let res = await FamilyBing.find().skip( (SkipPage - 1) * pageSize)
    if (res) {
        CtxBody(ctx, 200, {res, count})
    } else {
        CtxBody(ctx, 404, "")
    }
})
// --------------------------------------------------------------------------------------


// --------------------------------------------------------------- 药物过敏 -------------

// 添加药物过敏

router.post("/addGM", async ctx => {
    let { name, id } = ctx.request.body 
    if (id) {
        let data = await GM.findOne({ name })
        if (data) {
            CtxBody(ctx, 402, "")
        } else {
            let res = await GM.findOneAndUpdate({_id: id}, {
                $set: {
                    name
                }
            })
            CtxBody(ctx, 204, "")
        }
    } else {
        let gm = new GM({
            name
        })
        let ret = await gm.save()
        CtxBody(ctx, 202, "")
    }
})

// 显示或隐藏药物过敏

router.post("/changeGMStatus", async ctx => {
    let { id } = ctx.request.body 
    let man = await GM.findOne({ _id: id })
    let state = man.status
    if (state == 1) {
        let update = await GM.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "2"
            }
        })
        CtxBody(ctx, 204, "")
    } else {
        let update = await GM.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "1"
            }
        })
        CtxBody(ctx, 204, "")
    }
})

// 获取药物过敏

router.post("/getGM", async ctx => {
    let { page } = ctx.request.body 
    let SkipPage = page ? page : 1
    let pageSize = 10
    let count = await GM.count()
    let res = await GM.find().skip( (SkipPage - 1) * pageSize)
    if (res) {
        CtxBody(ctx, 200, {res, count })
    } else {
        CtxBody(ctx, 404, "")
    }
})
// --------------------------------------------------------------------------------------

// --------------------------------------------------------------- 食物过敏 -------------

// 添加食物过敏

router.post("/addFoodGM", async ctx => {
    let { name, id } = ctx.request.body 
    if (id) {
        let data = await FoodGM.findOne({ name })
        if (data) {
            CtxBody(ctx, 402, "")
        } else {
            let res = await FoodGM.findOneAndUpdate({_id: id}, {
                $set: {
                    name
                }
            })
            CtxBody(ctx, 204, "")
        }
    } else {
        let gm = new FoodGM({
            name
        })
        let ret = await gm.save()
        CtxBody(ctx, 202, "")
    }
})

// 显示或隐藏食物过敏

router.post("/changeFoodGMStatus", async ctx => {
    let { id } = ctx.request.body 
    let man = await FoodGM.findOne({ _id: id })
    let state = man.status
    if (state == 1) {
        let update = await FoodGM.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "2"
            }
        })
        CtxBody(ctx, 204, "")
    } else {
        let update = await FoodGM.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "1"
            }
        })
        CtxBody(ctx, 204, "")
    }
})

// 获取食物过敏

router.post("/getFoodGM", async ctx => {
    let { page } = ctx.request.body 
    let SkipPage = page ? page : 1
    let pageSize = 10
    let count = await FoodGM.count()
    let res = await FoodGM.find().skip( (SkipPage - 1) * pageSize)
    if (res) {
        CtxBody(ctx, 200, {res, count})
    } else {
        CtxBody(ctx, 404, "")
    }
})
// --------------------------------------------------------------------------------------


// --------------------------------------------------------------- 个人习惯 -------------

// 添加个人习惯

router.post("/addSelfDone", async ctx => {
    let { name, id } = ctx.request.body 
    if (id) {
        let data = await SelfDone.findOne({ name })
        if (data) {
            CtxBody(ctx, 402, "")
        } else {
            let res = await SelfDone.findOneAndUpdate({_id: id}, {
                $set: {
                    name
                }
            })
            CtxBody(ctx, 204, "")
        }
    } else {
        let gm = new SelfDone({
            name
        })
        let ret = await gm.save()
        CtxBody(ctx, 202, "")
    }
})

// 显示或隐藏个人习惯

router.post("/changeSelfDoneStatus", async ctx => {
    let { id } = ctx.request.body 
    let man = await SelfDone.findOne({ _id: id })
    let state = man.status
    if (state == 1) {
        let update = await SelfDone.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "2"
            }
        })
        CtxBody(ctx, 204, "")
    } else {
        let update = await SelfDone.findOneAndUpdate({ _id:id }, {
            $set: {
                status: "1"
            }
        })
        CtxBody(ctx, 204, "")
    }
})

// 获取个人习惯

router.post("/getSelfDone", async ctx => {
    let { page } = ctx.request.body 
    let SkipPage = page ? page : 1
    let pageSize = 10
    let count = await SelfDone.count()
    let res = await SelfDone.find().skip( (SkipPage - 1) * pageSize)
    if (res) {
        CtxBody(ctx, 200, {res, count})
    } else {
        CtxBody(ctx, 404, "")
    }
})
// --------------------------------------------------------------------------------------



//  添加 医院 科室


router.post("/addKeshi", async ctx => {
    let { name, status, id } = ctx.request.body 
    if (id) {
        let res = await KeShi.findOneAndUpdate({ _id: id }, {
            $set: {
                name,
                status
            }
        })
        CtxBody(ctx, 204, "")
    } else {
        let newkeshi = new KeShi({
            name,
            status
        })
        let ret = await newkeshi.save()
        if (ret) {
            CtxBody(ctx, 202, "")
        } else {
            CtxBody(ctx, 302, "")
        }
    }
})

// 获取医院科室

router.post("/getAllKeshi", async ctx => {
    let { page } = ctx.request.body
    let SkipPage = page ? page : 1
    let pageSize = 10
    let count = await KeShi.count()
    let res = await KeShi.find().skip( (SkipPage - 1) * pageSize ).limit( pageSize )
    if (res.length) {
        CtxBody(ctx, 200, {res, count})
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 医院科室 改变状态

router.post("/changeKeshiStatus", async ctx => {
    let { id } = ctx.request.body 
    if (id) {
        let res = await KeShi.findOne({ _id: id })
        if (res.status == 1) {
            let ret = await KeShi.findOneAndUpdate({ _id: id}, {
                $set: {
                    status: "2"
                }
            })
            CtxBody(ctx, 204,"")
        } else {
            let ret = await KeShi.findOneAndUpdate({ _id: id}, {
                $set: {
                    status: "1"
                }
            })
            CtxBody(ctx, 204,"")
        }
    } else {
        CtxBody(ctx, 30003, "")
    }
})



module.exports = router